package main.chapter4

fun main(args: Array<String>) {

}

data class ListNode(val value: Int, var next: ListNode?)

//尾递归优化
tailrec fun findNode(head: ListNode?, value: Int): ListNode? {
    head ?: return null
    return if (head.value == value) head
    else findNode(head.next, value)
}

//非尾递归
fun factorial(n: Long): Long {
    return n * factorial(n - 1)
}

data class TreeNode(val value: Int) {
    var left: TreeNode? = null
    var right: TreeNode? = null
}

//非尾递归
fun findTreeNode(root: TreeNode?, value: Int): TreeNode? {
    root ?: return null
    if (root.value == value) return root
    return findTreeNode(root.left, value) ?: findTreeNode(root.right, value)
}